from ultralytics import YOLO
import cv2


# 识别图片
def test1():
    img1 = cv2.imread("../photos/report.jpeg")
    cv2.imshow("anyone", img1)
    cv2.waitKey(delay=2000)

    # cv2.destroyAllWindows()

    model = YOLO('../yolo11x.pt')
    # model=YOLO('/home/xieweig/PycharmProjects/books/yolo11n.pt')
    res = model.predict(img1)
    # print(model)
    print(res)
    img2 = res[0].plot()
    cv2.imshow("anyone", img2)
    cv2.waitKey(delay=3000)

    cv2.destroyWindow("anyone")


def test2():
    model = YOLO('../yolo11n.pt')
    # model = YOLO('/home/xieweig/PycharmProjects/books/src/runs/detect/train2/weights/best.pt')

    origin = ['1018.jpg', '1021.jpg', '1023.jpg','1026.jpg','1036.jpg']
    cv2.namedWindow("someone", flags=cv2.WINDOW_KEEPRATIO)

    for name in origin:
        print(name)
        fullname = f'../photos/{name}'
        print(fullname)
        img1 = cv2.imread(fullname)
        cv2.imshow('someone', img1)
        cv2.waitKey(1000)
        res = model(img1)
        img2 = res[0].plot()
        cv2.imshow('someone', img2)
        cv2.waitKey(2000)

    cv2.destroyWindow('someone')


def test3():
    video_path = '/home/xieweig/PycharmProjects/books/photos/example.mp4'
    cv2.namedWindow("main",cv2.WINDOW_KEEPRATIO)
    video = cv2.VideoCapture(video_path)
    # video = cv2.VideoCapture(0)
    model = YOLO("/home/xieweig/PycharmProjects/books/yolo11n.pt")
    while True:
        flag, img1 = video.read()
        if not flag:
            break
        # cv2.imshow('main',img1)
        res = model.predict(img1)
        print(res)
        img2 = res[0].plot()
        cv2.imshow('main',img2)

        s =cv2.waitKey(100)
        # 按下esc就结束
        if(s == 27):
            break

    video.release()
    cv2.destroyWindow('main')


def test4():
    # 本质是一个深度学习的神经网络
    model = YOLO("/home/xieweig/PycharmProjects/books/yolo11.yaml")
    model.train(data='/home/xieweig/PycharmProjects/books/my-dataset.yaml',batch=5,epochs=120)
    model.val(data='/home/xieweig/PycharmProjects/books/my-dataset.yaml')

if __name__ == '__main__':
    test1()
